﻿namespace StarsAdmin.Core.Models
{
    public class ResultModel
    {
        /// <summary>
        /// 是否响应成功
        /// </summary>
        public bool IsSuccess { get; set; }

        /// <summary>
        /// 状态码
        /// </summary>
        public int Code { get; set; }

        /// <summary>
        /// 消息
        /// </summary>
        public string Message { get; set; } = "";
    }

    /// <summary>
    /// Http响应模型
    /// </summary>
    public class ResultModel<T> : ResultModel
    {
        /// <summary>
        /// 数据
        /// </summary>
        public T Data { get; set; }

        /// <summary>
        /// 成功啦
        /// </summary>
        /// <param name="data"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static ResultModel<T> Success(T data, string msg = "成功")
        {
            if (data == null)
            {
                return Error("出现了一些错误");
            }
            return new ResultModel<T> { Data = data, Message = msg, IsSuccess = true, Code = 200 };
        }

        /// <summary>
        /// 出错啦
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="code"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static ResultModel<T> Error(string msg = "失败", int code = 400, T data = default)
        {
            return new ResultModel<T> { Data = data, Message = msg, IsSuccess = false, Code = code };
        }
    }

    public static class ResultModelExtension
    {
        /// <summary>
        /// 返回结果格式化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <param name="errorMes"></param>
        /// <param name="errorCode"></param>
        /// <returns></returns>
        public static ResultModel<T> ResultFormat<T>(this T data, string errorMes = "失败", int errorCode = 400)
        {
            if (data != null)
            {
                return ResultModel<T>.Success(data);
            }
            return ResultModel<T>.Error(errorMes, errorCode);
        }

        /// <summary>
        /// 布尔类型返回结果格式化
        /// </summary>
        /// <param name="data"></param>
        /// <param name="errorMes"></param>
        /// <param name="errorCode"></param>
        /// <returns></returns>
        public static ResultModel<bool> ResultFormat(this bool data, string errorMes = "操作失败", int errorCode = 400)
        {
            if (data)
            {
                return ResultModel<bool>.Success(data);
            }
            return ResultModel<bool>.Error($"{errorMes}，请稍后重试", errorCode);
        }

        /// <summary>
        /// 集合类型数据返回结果格式化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <param name="errorMes"></param>
        /// <param name="errorCode"></param>
        /// <returns></returns>
        public static ResultModel<List<T>> ResultFormat<T>(this List<T> data, string errorMes = "没有找到数据", int errorCode = 404)
        {
            if (data != null && data.Count != 0)
            {
                return ResultModel<List<T>>.Success(data);
            }
            return ResultModel<List<T>>.Error(errorMes, errorCode);
        }

        /// <summary>
        /// 分页数据返回结果格式化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <param name="errorMes"></param>
        /// <param name="errorCode"></param>
        /// <returns></returns>
        public static ResultModel<Page<T>> ResultFormat<T>(this Page<T> data, string errorMes = "没有找到数据", int errorCode = 404)
        {
            if (data != null && data.PageSize > 0 && data.Items.Any())
            {
                return ResultModel<Page<T>>.Success(data);
            }
            return ResultModel<Page<T>>.Error(errorMes, errorCode);
        }
    }
}